﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System;

public class EventNode
{
    public MonoBase data;
    public EventNode next;

    public EventNode(MonoBase tmpMono)
    {
        this.data = tmpMono;
        this.next = null;
    }
}

public class ManagerBase : MonoBase
{
    public Dictionary<ushort, EventNode> EventTree = new Dictionary<ushort, EventNode>();


    /// <summary>
    /// 
    /// </summary>
    /// <param name="mono">要注册的脚本</param>
    /// <param name="args">注册多个msg</param>
    public void RegistMsg(MonoBase mono, params ushort[] args)
    {
        for (int i = 0; i < args.Length; i++)
        {
            EventNode tmp = new EventNode(mono);
            RegistMsg(args[i], tmp);
        }
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="">根据的id</param>
    /// <param name="node">node 链表</param>
    public void RegistMsg(ushort id, EventNode node)
    {
        if (!EventTree.ContainsKey(id))
        {
            EventTree.Add(id, node);
        }
        else
        {
            EventNode tmp = EventTree[id];
            while (tmp.next != null)
            {
                tmp = tmp.next;
            }
            //挂在最后一个上边
            tmp.next = node;
        }
    }

    public void UnRegistMsg(ushort id, MonoBase node)
    {
        if (EventTree.ContainsKey(id))
        {
            Debug.LogWarning("not contain id==" + id);
        }
        else
        {
            EventNode tmp = EventTree[id];

            if (tmp.data == node)
            {
                EventNode header = tmp;

                if (header.next != null)//后边有多个节点 
                {
                    header.data = tmp.next.data;
                    header.next = tmp.next.next;
                }
                else
                {
                    //只有一个节点 
                    EventTree.Remove(id);
                }
            }
            else//去掉尾部和中间
            {
                while (tmp.next != null && tmp.next.data != node)
                {
                    tmp = tmp.next;
                }

                if (tmp.next.data == node)//已找到节点 
                {
                    if (tmp.next.next != null)//去掉中间的
                    {
                        tmp.next = tmp.next.next;
                    }
                    else
                    {//去掉尾部
                        tmp.next = null; ;
                    }
                }
                else
                {
                    //没有找到节点 

                    Debug.LogWarning("not data in EventNode==" + id);
                }
            }

        }
    }

    /// <summary>
    /// 取消一个脚本的多个消息 
    /// </summary>
    /// <param name="mono"></param>
    /// <param name="msgs"></param>
    public void UnRegistMsg(MonoBase mono, params ushort[] msgs)
    {
        for (int i = 0; i < msgs.Length; i++)
        {
            UnRegistMsg(msgs[i], mono);
        }
    }

    public override void ProcessEvent(MsgBase tmpMsg)
    {
        if (!EventTree.ContainsKey(tmpMsg.msgId))
        {
            Debug.LogWarning("msg not contain msgid==" + tmpMsg.msgId);
            Debug.LogWarning("msg manager==" + tmpMsg.GetManager());
        }
        else
        {
            EventNode tmp = EventTree[tmpMsg.msgId];
            do
            {
                tmp.data.ProcessEvent(tmpMsg);
                tmp = tmp.next;//
            } while (tmp != null);
        }
    }
}
